package install.ren.exercise.test.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author zhuyunhui
 * @date 2020-05-20 11:35
 */
@Component
//@Order(99)
//@DependsOn("springBootXndLifeCycle")
public class SpringBootLifeCycle implements

//        BeanDefinitionRegistryPostProcessor,
//        BeanFactoryPostProcessor,
//        BeanPostProcessor,
        ApplicationContextAware, // 在BeanPostProcessor的before前
        InitializingBean,
        SmartLifecycle,
        ApplicationListener<ContextStartedEvent>,
        DisposableBean {

    private AtomicBoolean oneBefore = new AtomicBoolean();
    private AtomicBoolean oneAfter = new AtomicBoolean();

//    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("=================================BeanPostProcessor.before: " + beanName);
        return bean;
    }

//    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("=================================BeanPostProcessor.after: " + beanName);
        return bean;
    }

    public SpringBootLifeCycle() {
        System.out.println("=================================SpringBootLifeCycle()");
    }

    @PostConstruct
    public void init() {
        System.out.println("=================================PostConstruct");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("=================================ApplicationContextAware");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("=================================DisposableBean.destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("=================================InitializingBean");
    }

    @Override
    public void start() {
        System.out.println("=================================SmartLifecycle.start");
    }

    @Override
    public void onApplicationEvent(ContextStartedEvent contextStartedEvent) {
        System.out.println("=================================ApplicationListener<ContextStartedEvent>");
    }

    @Override
    public void stop() {
        System.out.println("SmartLifecycle.stop");
    }

    @Override
    public boolean isRunning() {
        return false;
    }

//    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("=================================BeanFactoryPostProcessor");

    }

//    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        System.out.println("=================================BeanDefinitionRegistryPostProcessor");
    }
}
